using System;
using UnityEngine;

// 站立
public class CharacterIdleState<T> : CharacterState<T> where T : CharacterBase<T>
{

    public CharacterIdleState(T character, CharacterStateMachine<T> stateMachine)
        : base(character, stateMachine) { }


    public override void Enter()
    {
        // character.CharacterAnimation.setMoveSpeed(0f);
        character.StopAllActions();

    }

    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        if (Player && iinputSource.IsJumping)
        {
            Player!.Jump(); // 转换为子类并调用

            stateMachine.ChangeState(new CharacterJumpState<T>(character, stateMachine));
            return;
        }
        if (iinputSource.leftClick)
        {
            stateMachine.ChangeState(new CharacterAttackState<T>(character, stateMachine));
            return;
        }

        if (iinputSource.MoveInput)
        {
            stateMachine.ChangeState(new CharacterMoveState<T>(character, stateMachine));
            return;
        }
        if (Player != null && Player.rb.velocity.y < -1 && !Player.IsFalling())
        {
            stateMachine.ChangeState(new CharacterJumpState<T>(character, stateMachine));
            return;
        }
        character.CharacterAnimation.Idle();
        // iinputSource.StopMove();
        // character.StopAllActions();

        // character.MoveCharacter();
    }
}

// 移动状态
public class CharacterMoveState<T> : CharacterState<T> where T : CharacterBase<T>
{
    public CharacterMoveState(T character, CharacterStateMachine<T> stateMachine)
        : base(character, stateMachine) { }


    public override void Enter()
    {
        character.setRotation();
        character.MoveCharacter();
    }

    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        if (Player && iinputSource.IsJumping)
        {
            Player.Jump();

            stateMachine.ChangeState(new CharacterJumpState<T>(character, stateMachine));
            return;
        }
        // if (character.die)
        // {
        //     stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine)); return;
        // }
        if (iinputSource.leftClick || iinputSource.rightHoldClick)
        {
            stateMachine.ChangeState(new CharacterAttackState<T>(character, stateMachine));
            return;
        }


        if (!iinputSource.MoveInput)
        {
            stateMachine.ChangeState(new CharacterIdleState<T>(character, stateMachine));
            return;
        }
        if (Player != null && character.rb.velocity.y < -1 && !Player.IsFalling())
        {
            stateMachine.ChangeState(new CharacterJumpState<T>(character, stateMachine));
            return;
        }
        // if (iinputSource.ShiftMoveInput)
        // {
        //     character.CharacterAnimation.PlayShiftMoveAnim();
        // }
        // else
        // {
        character.CharacterAnimation.PlayMoveAnim();
        // }
        character.MoveCharacter();
        iinputSource.StartMove();
    }
    public override void Exit()
    {
        character.setRotation();
    }
}

// 跳跃状态
public class CharacterJumpState<T> : CharacterState<T> where T : CharacterBase<T>
{

    public CharacterJumpState(T character, CharacterStateMachine<T> stateMachine)
        : base(character, stateMachine) { }

    public override void Enter()
    {
        if (Player)
            Player.setTouchEnemyComp();

    }


    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        //落地射线检测
        if (Player && Player!.IsFalling())
        {
            stateMachine.ChangeState(new CharacterIdleState<T>(character, stateMachine));
            return;
        }
        if (Player)
            Player.Jumping();

        character.MoveCharacter();
    }
    public override void Exit()
    {

    }
}

// 攻击状态
public class CharacterAttackState<T> : CharacterState<T> where T : CharacterBase<T>
{

    public CharacterAttackState(T character, CharacterStateMachine<T> stateMachine)
        : base(character, stateMachine) { }

    public override void Enter()
    {
        character.StopAllActions();
        character.CharacterAnimation.PlayAttack(true);
    }

    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        if (iinputSource.leftClick)
        {
            character.CharacterAnimation.PlayAttack(true);

        }
        if (!character.CharacterAnimation.isAttack)
        {
            stateMachine.ChangeState(new CharacterIdleState<T>(character, stateMachine));
        }
    }



    public override void Exit()
    {
    }
}

// 受击
public class CharacterHitState<T> : CharacterState<T> where T : CharacterBase<T>
{
    public CharacterHitState(T character, CharacterStateMachine<T> stateMachine)
        : base(character, stateMachine) { }
    public override void Enter()
    {
        character.StopAllActions();
        character.CharacterAnimation.Hit();
        iinputSource.StopMove();
        character.AttackRepel();
    }

    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        // 播放完就回到 Idle
        if (character.CharacterAnimation.IsCurrentAnimFinished("hit"))
        {
            stateMachine.ChangeState(new CharacterIdleState<T>(character, stateMachine));
        }
    }
}


// 闪避
public class CharacterDodgeState<T> : CharacterState<T> where T : CharacterBase<T>
{

    public CharacterDodgeState(T character, CharacterStateMachine<T> m) : base(character, m) { }

    public override void Enter()
    {
        // 朝向：有输入用输入方向，没输入用角色正前

    }

    public override void Update()
    {
        if (character.die)
        {
            stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine));
            return;
        }
        // 结束：动画播完或到时
        if (character.CharacterAnimation.IsCurrentAnimFinished("Dodge"))
        {
            stateMachine.ChangeState(new CharacterIdleState<T>(character, stateMachine));
        }
    }
}

// 死亡状态
internal class CharacterDieState<T> : CharacterState<T> where T : CharacterBase<T>
{
    public CharacterDieState(T character, CharacterStateMachine<T> stateMachine) : base(character, stateMachine)
    {
    }
    public override void Enter()
    {
        // Debug.Log("已死亡");
        character.CharacterAnimation.Die();
        character.CharacterDie();
    }
    public override void Update()
    {
        character.StopAllActions();
    }
}

// 战斗中 idle，监听是否释放法术/格挡/闪避等
// public class CharacterCombatIdleState<T> : CharacterState<T> where T : CharacterBase<T>
// {
//     public CharacterCombatIdleState(T character, CharacterStateMachine<T> stateMachine)
//        : base(character, stateMachine) { }
//     private float timer = 2f;  //倒计时
//     public override void Enter()
//     {
//         // character.animatorController.PlayCombatIdle();
//     }

//     public override void Update()
//     {
//         // if (character.die)
//         // {
//         //     stateMachine.ChangeState(new CharacterDieState<T>(character, stateMachine)); return;
//         // }
//         // if (input.IsCastPressed())
//         //     stateMachine.ChangeState(new PlayerCastSpellState());
//         // else if (input.IsDodgePressed())
//         //     stateMachine.ChangeState(new PlayerDodgeState());
//     }
// }

